<?php
namespace app\houtai\controller\user\room;
use app\houtai\controller\BaseController;
use think\Db;
use think\Request;

class PlayController extends BaseController {
  /**
   *  @brief  : 用户管理-游戏房间播放码
   *  @return : page
   *  @details: 
   */
  public function indexAction() {
    $conds = [];
    $order = ['CreateTimer'=>'DESC'];
    $GAME_KIND = GetGameKind();
    //////////////////////////////////////////////////////////////////////////////////////
    $kind = $this->request->param('kind/d', -1);
    if (array_key_exists($kind, $GAME_KIND)) {
      $conds['a.wKindID'] = $kind;
    } else {
      $kind = -1;
    }
    $search = $this->request->param('search/s', '', 'trim');
    if ($search > 0) {
      $conds['a.PrivateRoomID|a.PlayCodeID'] = $search;
    }
    ////////////////////////////
    $time1 = $this->request->param('time1/s', '', 'trim');
    $time2 = $this->request->param('time2/s', '', 'trim');
    
    $aa = YmdHis2Unix($time1);
    $bb = YmdHis2Unix($time2);
    $ta = date('Y-m-d 00:00:00', $aa);
    $tb = date('Y-m-d 23:59:59', $bb);
    if ($aa > 0) {
      if ($bb > 0) {
        if ($aa <= $bb) {
          $conds['a.CreateTimer'] = ['BETWEEN TIME', [$ta, $tb]];
        } else {
          $conds['a.CreateTimer'] = ['>= TIME', $ta];
        }
      } else {
        $conds['a.CreateTimer'] = ['>= TIME', $ta];
      }
    } else {
      if ($bb > 0) {
        $conds['a.CreateTimer'] = ['<= TIME', $tb];
      }
    }
    //////////////////////////////////////////////////////////////////////////////////////
    $list = table('CYRecordDB.dbo.PlayCodeToRoomId a', 'rydb.record')->field(['a.*', 'b.KindName'=>'KindName'])
            ->join('CYWebSMSDB.dbo.GameKindNote b', 'b.KindID = a.wKindID', 'LEFT')
            ->where($conds)->order($order)->paginate();
    //////////////////////////////////////////////////////////////////////////////////////
    return $this->fetch('', [
      'list'  => $list,
      'search'=> $search,
      'kind'  => $kind,
      'time1' => $time1,
      'time2' => $time2,
      'GAME_KIND' => $GAME_KIND,
    ]);
  }
}
